package cn.zjitc.exam.service.impl;

import cn.zjitc.exam.domain.Product;
import cn.zjitc.exam.mapper.ProductMapper;
import cn.zjitc.exam.service.ProductService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ProductServiceImpl implements ProductService {
    @Autowired
    private ProductMapper productMapper;
    @Override
    public List<Product> findAll() {
        return productMapper.findAll();
    }

    @Override
    public List<Product> findByPage(int page, int size) {
        int offset = (page - 1) * size;
        return productMapper.findByPage(offset, size);
    }

    @Override
    public int count() {
        return productMapper.count();
    }

    @Override
    public void save(Product product) {
        productMapper.save(product);
    }

    @Override
    public Product findById(Integer id) {
        return productMapper.findById(id);
    }

    @Override
    public void update(Product product) {
        productMapper.update(product);
    }

    @Override
    public void delete(Integer id) {
        productMapper.delete(id);
    }

    @Override
    public PageInfo<Product> list(String keyword, int page, int size) {
        PageHelper.startPage(page, size);
        List<Product> products = productMapper.search(keyword);
        PageInfo<Product> pageInfo = new PageInfo<>(products);
        pageInfo.setList( products);

        return pageInfo;
    }

    @Override
    public int countByKeyword(String keyword) {
        return productMapper.countByKeyword(keyword);
    }
}